package com.jic.member.service.eventHandler.impl.consume;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jic.common.base.tuple.Tuple;
import com.jic.member.entity.model.EventListenerContent;
import org.springframework.stereotype.Component;

import java.util.function.Predicate;

/**
 * description: PointsCashInHandler
 * 积分抵现
 * admin 参数[{"eventName":"积分抵现","eventRangeTime":{"f":1591689820000,"s":1593689820000},"pointsCashInAmount":{"f":1,"s":3},"pointsCashInNum":1}]
 *  * h5 参数 {"eventCode":"points_cash_in","eventTime":1591689920000,"eventType":1,"thirdUserId":"1","param":{pointsCashInAmount:2,"pointsCashInNum":1}}
 * date: 2020/6/8 11:40
 * author: wangys
 * version: 1.0
 */
@Component
public class PointsCashInHandler extends ConsumeEventHandlerTemplate {
    @Override
    public String getEventCode() {
        return "points_cash_in";
    }

    /**
     * 前置参数校验
     * @return
     */
    @Override
    public Predicate<JSONObject> preValidateParam() {
        return (t) -> {
            return t!= null && t.getInteger("pointsCashInAmount") != null && t.getInteger("pointsCashInNum") != null;
        };
    }

    @Override
    public Tuple<Boolean, String> validateParam(EventListenerContent content, Long branchNodeId) {
        Tuple<Boolean, String> match = new Tuple<>();
        match.setF(true);
        JSONArray jsonArray = parseNodeVariable.apply(content, branchNodeId);
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject param = (JSONObject)jsonArray.get(i);
            Tuple<Long, Long> eventRangeTime = param.getObject("eventRangeTime", Tuple.class);
            if(eventRangeTime != null && eventRangeTime.getF() != null){
                long start = eventRangeTime.getF();
                long end = eventRangeTime.getS();
                long now = System.currentTimeMillis();
                if(!(start <= now && now <= end)){
                    match.setF(false);
                    match.setS("时间条件不满足");
                    break;
                }
            }

            Tuple<Integer, Integer> pointsCashInAmount = param.getObject("pointsCashInAmount", Tuple.class);
            Integer startAmount = pointsCashInAmount.getF() == null ? Integer.MIN_VALUE : pointsCashInAmount.getF();
            Integer endAmount = pointsCashInAmount.getS() == null ? Integer.MAX_VALUE : pointsCashInAmount.getS();
            Integer cashInAmount = content.getParam().getInteger("pointsCashInAmount");
            if(!(startAmount <= cashInAmount && startAmount <= endAmount)){
                match.setF(false);
                match.setS("积分抵现金额不满足");
                break;
            }

            Integer pointsCashInNum = param.getInteger("pointsCashInNum");
            if(!pointsCashInNum.equals(content.getParam().getInteger("pointsCashInNum"))){
                match.setF(false);
                match.setS("积分抵现次数不满足");
                break;
            }
        }
        return match;
    }
}
